package cn.sc.summer.constant.runner;

import org.springframework.boot.CommandLineRunner;
import org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 类名：全局公用启动类
 *
 * @author a-xin
 * @date 2023/8/25 14:31
 */
@Component
public class CommonRunner implements CommandLineRunner, ApplicationListener<ContextClosedEvent> {

    @Override
    public void run(String... args) throws NoSuchMethodException {
    }

    @Override
    public void onApplicationEvent(@NonNull ContextClosedEvent event) {
        doDeregister();
    }

    @Resource
    private AbstractAutoServiceRegistration<?> abstractAutoServiceRegistration;

    @EventListener(ContextClosedEvent.class)
    public void doDeregister() {
        try {
            abstractAutoServiceRegistration.destroy();
        } catch (Exception ignored) {
        }
    }

}
